/*
 * @Description: 回收站上下文
 * @Author: Rfan
 * @Date: 2022-10-28 13:55:59
 * @LastEditTime: 2023-03-01 20:40:23
 */

import { Context, createContext, FC, useContext, useEffect, useReducer } from 'react';
import { Modal } from 'antd';
import { ArchiveBasketReducer } from '../reducers/ArchiveBasketReducer';
import { CLEAR_ALL, DELETE_RECYCLE_ITEM, RESTORE_ITEM, SET_RECYCLE_LIST } from '../constants';
import {
  // clearAllItem,
  delBasketById,
  fetchBasketByPage
  // restoreItem
} from '@/api/head/basket';
import { SET_SELECTED_ITEM } from '@/view/accession/BatchManage/constants';

// 初始化数据
const initialState = {
  selectItems: [],
  list: [],
  levelList: [
    { id: 0, name: '电子文件级' },
    { id: 1, name: '文件级' },
    { id: 2, name: '案卷级' },
    { id: 3, name: '项目级' }
  ]
};

// 生成context
const ArchiveBasketContext = createContext();

// provider高阶组件
const ArchiveBasketContextProvider = (props) => {
  const [state, dispatch] = useReducer(ArchiveBasketReducer, initialState);

  const actions = {
    /**
     * @description: 设置表格中选中条目的key
     * @param {number[]} selectArr
     */
    setSelectKey(selectArr) {
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },

    /**
     * @description: 获取移交清单列表
     */
    getArchiveBasketList(params) {
      return new Promise((resolve, reject) => {
        fetchBasketByPage(params)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_RECYCLE_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => reject(err));
      });
    },

    delRecycleItemFunc(id, content = '是否确认删除？') {
      Modal.confirm({
        title: '删除',
        content,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          delBasketById(ids).then((res) => {
            const data = res.data;
            dispatch({ type: DELETE_RECYCLE_ITEM, ids: data });
          });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    clearAll() {
      Modal.confirm({
        title: '删除',
        content: '确认清空回收站？',
        onOk() {
          // clearAllItem()
          //   .then(() => {
          //     dispatch({ type: CLEAR_ALL });
          //   })
          //   .catch(() => {});
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },

    restoreItemFunc(params) {
      return new Promise((resolve, reject) => {
        // restoreItem(params)
        //   .then((res) => {
        //     const ids = res.data || [];
        //     dispatch({ type: RESTORE_ITEM, ids });
        //     resolve(ids);
        //   })
        //   .catch((err) => reject(err));
      });
    }
  };

  const filters = {
    levelFilter(id) {
      return state.levelList.find((level) => level.id === id)?.name || '-';
    }
  };
  useEffect(() => {}, []);

  return (
    <ArchiveBasketContext.Provider value={{ ...state, ...actions, ...filters }}>
      {props.children}
    </ArchiveBasketContext.Provider>
  );
};

export const useArchiveBasketContext = () => {
  return useContext(ArchiveBasketContext);
};

export { ArchiveBasketContext, ArchiveBasketContextProvider };
